package cn.jobstart.controller;
import cn.jobstart.common.StringUtil;
import cn.jobstart.es.dto.CompanyEsPageDTO;
import cn.jobstart.es.dto.ResumeEsPageDTO;
import cn.jobstart.es.model.CompanyEs;
import cn.jobstart.es.model.ResumeEs;
import cn.jobstart.es.search.CompanyEsSearch;
import cn.jobstart.es.search.ResumeEsSearch;
import cn.jobstart.es.service.ICompanyEsService;
import cn.jobstart.es.vo.ResumeEsListVO;
import cn.jobstart.es.vo.ResumeEsVO;
import cn.jobstart.pubInter.common.PageBean;
import cn.jobstart.web.common.SkJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.pubInter.RestfulController;
import cn.jobstart.dto.CompanyDTO;
import cn.jobstart.dto.CompanyPageDTO;
import cn.jobstart.modelExt.CompanyExt;
import cn.jobstart.searchExt.CompanySearchExt;
import cn.jobstart.service.ICompanyService;
import cn.jobstart.vo.CompanyDetailVO;
import cn.jobstart.vo.CompanyListVO;
import cn.jobstart.vo.CompanyDomainVO;
import cn.jobstart.vo.CompanyDetailDomainVO;
import cn.jobstart.export.CompanyDomainExport;
import cn.jobstart.controller.ApiManageController;
import org.springframework.validation.annotation.Validated;
import java.util.Set;
import java.util.HashSet;
import cn.jobstart.pubInter.exception.BusinessException;

/**
*    @author sunke
*    @Date 2023-09-06 15:00:20
*    @description CompanyController      企业
*/
@Api(tags = "人才管理系统-企业相关", description = "企业相关接口")
@RestController(value ="companyRestfulController")
@RequestMapping("/company/")
@Validated
public class CompanyController extends ApiManageController<CompanyDTO,CompanyPageDTO,CompanyExt, CompanySearchExt,CompanyDomainVO,CompanyDetailDomainVO,CompanyDetailVO,CompanyListVO,String> {

	@Resource(name = "companyService")
	private ICompanyService companyService;

	@Resource
	private ICompanyEsService companyEsService;



	@ApiOperation(value = "企业Es列表", notes = "企业Es列表")
	@PostMapping("companyEsList")
	public SkJsonResult<CompanyListVO> companyEsList(@RequestBody CompanyEsPageDTO pageDTO) throws SkException {

		CompanyEsSearch search=map(pageDTO,CompanyEsSearch.class);

		EsPageInfo<CompanyEs> pageInfo = companyEsService.pageQuery(search, search.getPageNumber(), search.getPageSize());
		if (StringUtil.isNullOrEmpty(pageInfo) || StringUtil.isNullOrEmpty(pageInfo.getList())) {
			return SkJsonResult.ok();
		}
		PageBean page = new PageBean();
		page.setTotalCount(pageInfo.getTotal());
		page.setPageSize(search.getPageSize());
		long totalPage = page.getTotalPage();
		long pageNumber = search.getPageNumber();
		if (pageNumber > totalPage) {
			pageNumber = totalPage;
		}
		page.setCurrentPage(pageNumber);
		CompanyListVO vo = new CompanyListVO();
		EsPageInfo<CompanyEs> result = new EsPageInfo<>();
		map(pageInfo, result);
		vo.setList(mapList(pageInfo.getList(), CompanyDetailDomainVO.class));
		vo.setPage(page);
		return SkJsonResult.ok().setData(vo);

	}



	@Override
	public IBaseService<CompanyExt, CompanySearchExt,String> getBaseService() throws SkException {
		return companyService;
	}

	@Override
	public String getBaseRoute() {
		return "/company/";
	}

	/**
	* 当前模板名称
	* @return
	*/
	@Override
	public String getModelName(){
		return "企业";
	}

	@Override
	public Class exportExcelClass() {
		return CompanyDomainExport.class;
	}

	@Override
	public Set<String> totalMapSet() {
		Set totalMapSet=new HashSet();
		totalMapSet.add("count(company.c_id) as countNum");
		return totalMapSet;
	}


}